package com.hinnotek.amoeba.ui.fragment

import androidx.fragment.app.viewModels
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.StatisticalCustomerProjectAdapter
import com.hinnotek.amoeba.bean.old.AmibaCustomerProject
import com.hinnotek.amoeba.bean.old.StatisticalCustomerProjectBean
import com.hinnotek.amoeba.databinding.FragmentStatisticalCustomerProjectBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.ManageViewModel
import com.lee.common.base.BaseFragment
import com.lee.common.network.net.ObserverState
import com.xuexiang.xui.utils.WidgetUtils
import dagger.hilt.android.AndroidEntryPoint

/**
 *
 */
@AndroidEntryPoint
class FragmentStatisticalCustomerProject :
    BaseFragment<FragmentStatisticalCustomerProjectBinding>() {
    private var adapter: StatisticalCustomerProjectAdapter? = null
    private var orgId = 0
    private val dataList: MutableList<AmibaCustomerProject> = mutableListOf()

    val viewModel by viewModels<ManageViewModel>()

    override fun initData() {
        WidgetUtils.initRecyclerView(mBinding.rlv, 0)
        mBinding.rlv.adapter = StatisticalCustomerProjectAdapter(dataList).also { adapter = it }
        getAmibaCustomerDetail(orgId)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_statistical_customer_project
    }


    fun setOrgId(orgId: Int) {
        this.orgId = orgId
    }


    /**
     * 获取统计详情中的客户项目接口
     */
    private fun getAmibaCustomerDetail(orgId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getAmibaCustomerDetail(orgId).observe(this, object :
            ObserverState<MutableList<StatisticalCustomerProjectBean>>() {
            override fun onDataChange(data: MutableList<StatisticalCustomerProjectBean>) {
                customDialog.dismiss()
                for (bean in  data) {
                    val element = AmibaCustomerProject()
                    element.type = 0
                    element.customerName = bean.customerName
                    dataList.add(element)
                    dataList.addAll(bean.amibaCustomerProject!!)
                }
                adapter?.notifyDataSetChanged()
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }

}